package kr.co.cherryt.activity;

import java.util.Timer;
import java.util.TimerTask;

import kr.co.cherryt.cherryt.R;
import kr.co.cherryt.common.CC_Log;
import kr.co.cherryt.common.CherrytData;
import kr.co.cherryt.common.Utility;
import kr.co.cherryt.receiver.PushWakeLock;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.AbsListView.LayoutParams;

public class ShowC2dmMsg extends Activity {
	
	protected String m_mode = null;
	protected String m_param = null;
	
	protected int nIntentFlag = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         
        LinearLayout ll = new LinearLayout(ShowC2dmMsg.this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        ll.setBackgroundResource(R.drawable.intro);
        setContentView(ll);
         
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED       // Lock화면 띄우기 전에 자신의 화면을 먼저 띄운다.
        	| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
        	| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
         
        Bundle bun = getIntent().getExtras();
        String title = bun.getString("title");
        String msg = bun.getString("msg");
        m_mode = bun.getString("mode");
        m_param = bun.getString("param");
        
        String szBtnText = null;
       
         
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(ShowC2dmMsg.this);
        
        // 일반 푸시
        if (Utility.IsEmpty(m_mode) || m_mode.equals("N")) {
        	szBtnText = "확인";
        	nIntentFlag = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP;
        }
        // 광고 바로 보기 푸시
        else if (m_mode.equals("M")) {
        	szBtnText = "광고 보기";
        	nIntentFlag = Intent.FLAG_ACTIVITY_CLEAR_TOP;
        }
        // 앱 실행 푸시
        else if (m_mode.equals("A")) {
        	szBtnText = "앱 실행";
        	nIntentFlag = Intent.FLAG_ACTIVITY_CLEAR_TOP;
        }
         
        alertDialog.setPositiveButton(szBtnText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            	Intent intent = new Intent();
            	intent.setClassName(getPackageName(), "kr.co.cherryt.activity.CherryTActivity");
            	intent.putExtra("c2dmMode", m_mode);
            	intent.putExtra("c2dmParam", m_param);
            	intent.addFlags(nIntentFlag);
                startActivity(intent);
                PushWakeLock.releaseCpuLock();
                
                NotificationManager notificationManager = (NotificationManager)ShowC2dmMsg.this.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(CherrytData.C2DM_NOTIFICATION_ID);
                
                ShowC2dmMsg.this.finish();
            }
        });
        
        
        alertDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                PushWakeLock.releaseCpuLock();
                ShowC2dmMsg.this.finish();
            }
        });
         
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        alertDialog.show();
 
     // 폰 설정의 조명시간을 가져와서 해당 시간만큼만 화면을 켠다.
        long defTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 15000);
        TimerTask task = new TimerTask() {
			@Override
			public void run() {
				PushWakeLock.releaseCpuLock();
			}
        };
        
        if (defTimeOut == -1) {
        	defTimeOut = 15000;
        }
             
        Timer timer = new Timer();
        timer.schedule(task, defTimeOut);
	}

}
